package com.king.headline.fragment;

/**
 * Created by king on 2015/12/1.
 */

import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.king.headline.R;
import com.king.headline.adapter.MainAdapter;
import com.king.headline.base.BaseFragment;
import com.king.headline.beans.MainInfo;
import com.king.headline.beans.UserInfo;
import com.king.headline.net.Callback;
import com.king.headline.net.NetWork;
import com.king.headline.util.AccountManager;
import com.king.headline.util.CustomToast;
import com.king.headline.view.viewPager.OnMyListener;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class TwoFragment extends BaseFragment {
    @BindView(R.id.lv_main)
    PullToRefreshListView mRlListView;
    @BindView(R.id.tv_none)
    TextView tv_none;
    private ListView lv_home;
    private MainAdapter adapter;
    private int index = 0;
    private boolean isRefresh;
    private boolean isLoadMore;
    private ArrayList<MainInfo> mainInfoListAll = new ArrayList<MainInfo>();
    private UserInfo userInfo;
    private boolean toClearn;

    @Override
    public View initView() {
        userInfo = AccountManager.getInstance().getUser();
        View view = View.inflate(mActivity, R.layout.activity_two, null);
        if (userInfo == null) {
            return view;
        }
        ButterKnife.bind(this, view);
        lv_home = mRlListView.getRefreshableView();
        mRlListView.setMode(PullToRefreshBase.Mode.BOTH);
        mRlListView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                isRefresh = true;
                toClearn = true;
                index = 0;// 页面重置
                getMainFromNet();
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                isLoadMore = true;
                if (mainInfoListAll != null && mainInfoListAll.size() > 0) {
                    index = mainInfoListAll.get(mainInfoListAll.size() - 1).index_;
                }
                getMainFromNet();
            }

        });

        return view;
    }

    @Override
    public void initData() {
//        getMainFromNet();
    }

    @Override
    public void onResume() {
        super.onResume();
//        if (mainInfoListAll!=null&&mainInfoListAll.size()>0) {
            toClearn = true;
            index=0;
            getMainFromNet();
//        }

    }

    public void getMainFromNet() {
        userInfo = AccountManager.getInstance().getUser();
        if (userInfo == null) {
            return;
        }
        NetWork.getInstance().getCollection(userInfo.encyptdId, userInfo.apikey, index, new Callback() {
            @Override
            public void success(String result) {
                RefreshComplete();
                if (toClearn){
                    if (mainInfoListAll!=null) {
                        mainInfoListAll.clear();
                        toClearn = false;
                    }
                }
                ArrayList<MainInfo> mainInfoList = new Gson().fromJson(
                        result,
                        new TypeToken<List<MainInfo>>() {
                        }.getType());
                if (mainInfoList == null || mainInfoList.size() == 0) {
                    if (mainInfoListAll==null||mainInfoListAll.size()==0) {
                       // CustomToast.showToast("没有更多数据了");
                        tv_none.setVisibility(View.VISIBLE);
                        lv_home.setVisibility(View.GONE);
                    }

                } else {

                    mainInfoListAll.addAll(mainInfoList);
                    tv_none.setVisibility(View.GONE);
                    lv_home.setVisibility(View.VISIBLE);
                    if (adapter == null) {
                        adapter = new MainAdapter(mActivity, mainInfoListAll);
                        lv_home.setAdapter(adapter);
                    } else {
                        adapter.setDate(mainInfoListAll);
                    }
                    adapter.isShowCollection(true);
                    adapter.setCollectChangeListener(new OnMyListener() {
                        @Override
                        public void method() {
                            toClearn = true;

                            getMainFromNet();
                        }
                    });

                }

            }

            @Override
            public void notice(JSONObject jsonObject, String message) {
                CustomToast.showToast(message);
                RefreshComplete();
            }

            @Override
            public void failed(String e) {
                CustomToast.showToast(e);
                RefreshComplete();

            }
        });

    }

    private void RefreshComplete() {
        if (mRlListView != null) {
            mRlListView.onRefreshComplete();
        }
        isRefresh = false;
        isLoadMore = false;
    }

}

